package com.itboye.textlieexpters.webview;

import java.io.FileNotFoundException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.util.CustomDialog;
import com.itboye.textileexpres.util.DownPicUtil;

public class ProcdeuWebView extends BaseActivity {

	WebView webview;
	Context context;

	TextView title;

	String imgUrl;
	String id;
	ImageView back;
	private String url;
	 WebView.HitTestResult hitTestResult;
	private ArrayAdapter<String> adapter;
	private CustomDialog mCustomDialog;
	ImageView img_other;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_web;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		webview = (WebView) findViewById(R.id.webview);
		title.setText(getIntent().getStringExtra("title"));
		id = getIntent().getStringExtra("id");
		webview.setHorizontalScrollBarEnabled(false);// 水平不显示
		webview.setVerticalScrollBarEnabled(false); // 垂直不显示
		url = getIntent().getStringExtra("url");
		imgUrl = getIntent().getStringExtra("img");
		context = this;
		initData();
		initListener();
		
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	

	private void initListener() {

		// 长按点击事件
		webview.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				 hitTestResult = webview
						.getHitTestResult();
				// 如果是图片类型或者是带有图片链接的类型
				if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE
						|| hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
					// 弹出保存图片的对话框
					
				
				showDialog();

				}
				return true;
			}
		});

		webview.loadUrl(url);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			String picFile = (String) msg.obj;
			String[] split = picFile.split("/");
			String fileName = split[split.length - 1];
			try {
				MediaStore.Images.Media.insertImage(getApplicationContext()
						.getContentResolver(), picFile, fileName, null);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			// 最后通知图库更新
			getApplicationContext().sendBroadcast(
					new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
							.parse("file://" + picFile)));
			Toast.makeText(context, "图片保存图库成功"+picFile, Toast.LENGTH_LONG).show();
		}
	};

	// 监听返回键返回网页的上一层
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
			if (webview != null) {
				webview.goBack();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	 private void  showDialog() {
	    	
	    	adapter = new ArrayAdapter<String>(this,R.layout.item_dialog);  
	        adapter.add("保存到手机"); 
	        adapter.add("取消");
	    	
	    	mCustomDialog = new CustomDialog(this, R.layout.custom_dialog) {
				
				@Override
				public void initViews() {
					// 初始CustomDialog化控件
					ListView mListView = (ListView) findViewById(R.id.lv_dialog);
			        mListView.setAdapter(adapter); 
			        mListView.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
							// 点击事件
							switch (position) {
							case 0:
								
								String url = hitTestResult.getExtra();
								// 下载图片到本地
								DownPicUtil
										.downPic(
												url,
												new DownPicUtil.DownFinishListener() {

													@Override
													public void getDownPath(
															String s) {
														Toast.makeText(
																context,
																"下载完成",
																Toast.LENGTH_LONG)
																.show();
														Message msg = Message
																.obtain();
														msg.obj = s;
														handler.sendMessage(msg);
													}
												});
								closeDialog();
								
								break;
							case 1:
								closeDialog();
								break;
							}
							
						}
					});
				}
			};
			mCustomDialog.show();
	    }
	    
	private void initData() {
		WebSettings settings = webview.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);// 设置此属性，可任意比例缩放
		settings.setLoadWithOverviewMode(true);
		// 使页面支持缩放
		settings.setBuiltInZoomControls(true);
		settings.setSupportZoom(true);
		// 支持自动加载图片
		settings.setLoadsImagesAutomatically(true);
		settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);// 排版适应屏幕
		// 缩放按钮
		settings.setDisplayZoomControls(false);

	}

	

}
